//
//  OvalView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/3/9.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class OvalView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let shapLayer = CAShapeLayer()
        let cornersRadius = CornersRadius(topLeft: 10, topRight: 10, bottomLeft: 0, bottomRight: 0)
        shapLayer.path = createPath(bounds: self.bounds, cornersRadius: cornersRadius)
        self.layer.mask = shapLayer
        self.backgroundColor = UIColor.white
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   typealias CornersRadius = (topLeft: CGFloat, topRight: CGFloat, bottomLeft: CGFloat, bottomRight: CGFloat)

    //创建Path
    func createPath(bounds:CGRect, cornersRadius:CornersRadius) -> CGPath {
            let minX = bounds.minX
            let minY = bounds.minY
            let maxX = bounds.maxX
            let maxY = bounds.maxY

            let topLeftCenterX = minX + cornersRadius.topLeft
            let topLeftCenterY = minY + cornersRadius.topLeft
            let topRightCenterX = maxX - cornersRadius.topRight
            let topRightCenterY = minY + cornersRadius.topRight
            let bottomLeftCenterX = minX + cornersRadius.bottomLeft
            let bottomLeftCenterY = maxY - cornersRadius.bottomLeft
            let bottomRightCenterX = maxX - cornersRadius.bottomRight
            let bottomRightCenterY = maxY - cornersRadius.bottomRight
            
            let path = CGMutablePath()
            path.addArc(center: CGPoint(x: topLeftCenterX, y: topLeftCenterY), radius: cornersRadius.topLeft, startAngle: CGFloat(Double.pi), endAngle: CGFloat(3 * Double.pi / 2.0), clockwise: false)
            path.addArc(center: CGPoint(x: topRightCenterX, y: topRightCenterY), radius: cornersRadius.topRight, startAngle: CGFloat(3 * Double.pi / 2.0), endAngle: 0, clockwise: false)
            path.addArc(center: CGPoint(x: bottomRightCenterX, y: bottomRightCenterY), radius: cornersRadius.bottomRight, startAngle: 0, endAngle: CGFloat(Double.pi / 2.0), clockwise: false)
            path.addArc(center: CGPoint(x: bottomLeftCenterX, y: bottomLeftCenterY), radius: cornersRadius.bottomLeft, startAngle: CGFloat(Double.pi / 2.0), endAngle: CGFloat(Double.pi), clockwise: false)
            path.closeSubpath()
            
            return path
    }
}
